<?php
class Question extends AppModel {
	var $name = 'Question';
	var $displayField = 'title';
	const UNSOLVED = 0;
	const CLOSED = 1;
	const SOLVED = 2;
	
	var $validate = array(
		'user_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'title' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'body' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Language' => array(
		    'className' => 'Language',
		    'foreignKey' => 'language_id',
	    )
	);

	var $hasMany = array(
		'Answer' => array(
			'className' => 'Answer',
			'foreignKey' => 'question_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	
	
	function is_solved($question_id = null)
	{

		$question = $this->Question->findById($question_id);
		if (!is_null($question) and $question['Question']['status']==Question::SOLVED) {
			return true;
		}else{
			return false;
		}
	}
	
	function status($question)
	{
		switch ($question['Question']['status']) {
			case Question::SOLVED:
				return '已解决';
				break;
			case Question::CLOSED:
				return '已关闭';
				break;
			case Question::UNSOLVED:
				return '待解决';
				break;
			
			default:
				return '待解决';
				break;
		}
	}

}
?>